import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Anagram {

    private final String diaper;
    private final String[] diaperString;

    Anagram(String diaper) {
        this.diaper = diaper.toLowerCase();
        this.diaperString = this.diaper.split("");
    }

    List<String> match(List<String> inputStrings) {
        List<String> results = new ArrayList<>();
        for (String input : inputStrings) {
            final String lowerInput = input.toLowerCase();
            if (lowerInput.equals(this.diaper) || lowerInput.length() != this.diaper.length())
                continue;

            final Map<String, Integer> diaperMap = new HashMap<>();
            Arrays.stream(this.diaperString).forEach((String s) -> {
                diaperMap.merge(s, 1, Integer::sum);
            });

            Arrays.stream(lowerInput.split("")).forEach(s -> {
                if (this.diaper.contains(s)) {
                    diaperMap.merge(s, -1, Integer::sum);
                }
            });

            final boolean[] canAdd = {true};
            diaperMap.values().forEach((Integer v) -> {
                if (v > 0) canAdd[0] = false;
            });

            if (canAdd[0]) results.add(input);
        }

        return results;
    }
}